<template>
  <div class="person">
    <h1>监视多个数据</h1>
    <h2>姓名:{{ person.name }}</h2>
    <h2>年龄:{{ person.age }}</h2>
    <h2>年龄:{{ person.age }}</h2>
    <h2>汽车:{{ person.car.c1 }}、{{ person.car.c2 }}</h2>
    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changeFirstCar">修改第一辆车</button>
    <button @click="changeTwoCar">修改第二辆车</button>
    <button @click="changeAllCar">修改整个车</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import { ref, watch, reactive } from "vue";

let person = reactive({
  name: "张三",
  age: 20,
  car: {
    c1: "奔驰",
    c2: "宝马",
  },
});

function changeName() {
  person.name = "zhangsan";
}
function changeAge() {
  person.age += 1;
}
function changePerson() {
  Object.assign(person, {
    name: "里斯",
    age: 90,
  });
}

function changeFirstCar() {
  person.car.c1 = "压低";
}

function changeTwoCar() {
  person.car.c2 = "压低2112";
}

function changeAllCar() {
  person.car = {
    c1: "呀低压",
    c2: "小刀",
  };
}

// 监视多个数据
watch(
  [() => person.name, () => person.car.c1, person.car],
  (newVal, oldVal) => {
    console.log("person变换了", newVal, oldVal);
  },
  { deep: true }
);
</script>

<style>
.person {
  width: 100%;
  background-color: skyblue;
}
</style>
